package reifnsk.minimap;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import net.minecraft.src.Entity;
import net.minecraft.src.FontRenderer;
import net.minecraft.src.GameSettings;
import net.minecraft.src.Minecraft;
import net.minecraft.src.Render;
import net.minecraft.src.RenderManager;
import net.minecraft.src.ResourceLocation;
import net.minecraft.src.Tessellator;
import org.lwjgl.opengl.GL11;
import reifnsk.minimap.WaypointEntityRender$ViewWaypoint;

public class WaypointEntityRender extends Render
{
    static final ReiMinimap rm = ReiMinimap.instance;
    final Minecraft mc;
    double far = 1.0D;
    double _d = 1.0D;
    static final boolean optifine;
    static int ofRenderDistanceFine;

    public WaypointEntityRender(Minecraft mc)
    {
        this.mc = mc;
    }

    /**
     * Actually renders the given argument. This is a synthetic bridge method, always casting down its argument and then
     * handing it off to a worker function which does the actual work. In all probabilty, the class Render is generic
     * (Render<T extends Entity) and this method has signature public void doRender(T entity, double d, double d1,
     * double d2, float f, float f1). But JAD is pre 1.5 so doesn't do that.
     */
    public void doRender(Entity entity, double d, double d1, double d2, float f, float f1)
    {
        if (optifine)
        {
            if (ofRenderDistanceFine != this.mc.gameSettings.ofRenderDistanceFine)
            {
                ofRenderDistanceFine = this.mc.gameSettings.ofRenderDistanceFine;
                this.far = (double)ofRenderDistanceFine * 1.6D;
                this._d = 1.0D / (double)ofRenderDistanceFine;
            }
        }
        else
        {
            this.far = (double)(512 >> this.mc.gameSettings.renderDistance) * 0.8D;
            this._d = 1.0D / (double)(256 >> this.mc.gameSettings.renderDistance);
        }

        double dscale = rm.getVisibleDimensionScale();
        ArrayList list = new ArrayList();

        if (rm.getMarker())
        {
            Iterator i$ = rm.getWaypoints().iterator();

            while (i$.hasNext())
            {
                Waypoint w = (Waypoint)i$.next();

                if (w.enable)
                {
                    list.add(new WaypointEntityRender$ViewWaypoint(this, w, dscale));
                }
            }

            if (!list.isEmpty())
            {
                Collections.sort(list);
                this.mc.entityRenderer.disableLightmap(0.0D);
                GL11.glDisable(GL11.GL_LIGHTING);
                GL11.glDisable(GL11.GL_FOG);
                i$ = list.iterator();

                while (i$.hasNext())
                {
                    WaypointEntityRender$ViewWaypoint w1 = (WaypointEntityRender$ViewWaypoint)i$.next();
                    this.draw(w1, f, f1);
                }

                GL11.glEnable(GL11.GL_FOG);
                GL11.glEnable(GL11.GL_LIGHTING);
                this.mc.entityRenderer.enableLightmap(0.0D);
                this.shadowSize = 0.0F;
            }
        }
    }

    void draw(WaypointEntityRender$ViewWaypoint w, float f, float f1)
    {
        float alpha = (float)Math.max(0.0D, 1.0D - w.distance * this._d);
        FontRenderer fontrenderer = this.getFontRendererFromRenderManager();
        GL11.glPushMatrix();
        StringBuilder sb = new StringBuilder();

        if (rm.getMarkerLabel() && w.name != null)
        {
            sb.append(w.name);
        }

        if (rm.getMarkerDistance())
        {
            if (sb.length() != 0)
            {
                sb.append(" ");
            }

            sb.append(String.format("[%1.2fm]", new Object[] {Double.valueOf(w.distance)}));
        }

        String str = sb.toString();
        double scale = (w.dl * 0.1D + 1.0D) * 0.02666666666666667D;
        int slideY = rm.getMarkerIcon() ? -16 : 0;
        GL11.glTranslated(w.dx, w.dy, w.dz);
        GL11.glRotatef(-this.renderManager.playerViewY, 0.0F, 1.0F, 0.0F);
        GL11.glRotatef(this.mc.gameSettings.thirdPersonView == 2 ? -this.renderManager.playerViewX : this.renderManager.playerViewX, 1.0F, 0.0F, 0.0F);
        GL11.glScaled(-scale, -scale, scale);
        GL11.glEnable(GL11.GL_BLEND);
        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
        Tessellator tessellator = Tessellator.instance;

        if (rm.getMarkerIcon())
        {
            GL11.glEnable(GL11.GL_TEXTURE_2D);
            GL11.glDisable(GL11.GL_DEPTH_TEST);
            GL11.glDepthMask(false);
            Waypoint.FILE[w.type].bind();
            tessellator.startDrawingQuads();
            tessellator.setColorRGBA_F(w.red, w.green, w.blue, 0.4F);
            tessellator.addVertexWithUV(-8.0D, -8.0D, 0.0D, 0.0D, 0.0D);
            tessellator.addVertexWithUV(-8.0D, 8.0D, 0.0D, 0.0D, 1.0D);
            tessellator.addVertexWithUV(8.0D, 8.0D, 0.0D, 1.0D, 1.0D);
            tessellator.addVertexWithUV(8.0D, -8.0D, 0.0D, 1.0D, 0.0D);
            tessellator.draw();
            GL11.glEnable(GL11.GL_DEPTH_TEST);
            GL11.glDepthMask(true);
            tessellator.startDrawingQuads();
            tessellator.setColorRGBA_F(w.red, w.green, w.blue, alpha);
            tessellator.addVertexWithUV(-8.0D, -8.0D, 0.0D, 0.0D, 0.0D);
            tessellator.addVertexWithUV(-8.0D, 8.0D, 0.0D, 0.0D, 1.0D);
            tessellator.addVertexWithUV(8.0D, 8.0D, 0.0D, 1.0D, 1.0D);
            tessellator.addVertexWithUV(8.0D, -8.0D, 0.0D, 1.0D, 0.0D);
            tessellator.draw();
        }

        int j = fontrenderer.getStringWidth(str) >> 1;

        if (j != 0)
        {
            GL11.glDisable(GL11.GL_TEXTURE_2D);
            GL11.glDisable(GL11.GL_DEPTH_TEST);
            GL11.glDepthMask(false);
            tessellator.startDrawingQuads();
            tessellator.setColorRGBA_F(0.0F, 0.0F, 0.0F, 0.6275F);
            tessellator.addVertex((double)(-j - 1), (double)(slideY - 1), 0.0D);
            tessellator.addVertex((double)(-j - 1), (double)(slideY + 8), 0.0D);
            tessellator.addVertex((double)(j + 1), (double)(slideY + 8), 0.0D);
            tessellator.addVertex((double)(j + 1), (double)(slideY - 1), 0.0D);
            tessellator.draw();
            GL11.glEnable(GL11.GL_TEXTURE_2D);
            GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
            fontrenderer.drawString(str, -j, slideY, w.type == 0 ? 1627389951 : 1627324416);
            GL11.glEnable(GL11.GL_DEPTH_TEST);
            GL11.glDepthMask(true);
            int a = (int)(255.0F * alpha);

            if (a != 0)
            {
                fontrenderer.drawString(str, -j, slideY, (w.type == 0 ? 16777215 : 16711680) | a << 24);
            }
        }

        GL11.glDisable(GL11.GL_BLEND);
        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
        GL11.glEnable(GL11.GL_TEXTURE_2D);
        GL11.glPopMatrix();
    }

    protected ResourceLocation func_110775_a(Entity var1)
    {
        return null;
    }

    static RenderManager access$000(WaypointEntityRender x0)
    {
        return x0.renderManager;
    }

    static RenderManager access$100(WaypointEntityRender x0)
    {
        return x0.renderManager;
    }

    static RenderManager access$200(WaypointEntityRender x0)
    {
        return x0.renderManager;
    }

    static
    {
        boolean b = false;

        try
        {
            GameSettings.class.getField("ofRenderDistanceFine");
            Class.forName("GuiPerformanceSettingsOF");
            b = true;
        }
        catch (Exception var2)
        {
            ;
        }

        optifine = b;
    }
}
